package com.woniuxy.cardservice.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sun.corba.se.spi.ior.iiop.IIOPFactories;
import com.woniuxy.carddao.mapper.ValidCardMapper;
import com.woniuxy.carddao.model.ValidCard;
import com.woniuxy.cardservice.exception.CardException;
import com.woniuxy.cardservice.service.ValidCardService;
import com.woniuxy.core.status.CardState;
import com.woniuxy.model.dto.ValidCardDto;
import com.woniuxy.model.form.AddValidCardForm;
import com.woniuxy.model.form.UpdateValidCardForm;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * <p>
 * 有效期会员卡表 服务实现类
 * </p>
 *
 * @author 冉杰
 * @since 2024-01-15
 */
@Service
public class ValidCardServiceImpl extends ServiceImpl<ValidCardMapper, ValidCard> implements ValidCardService {

    /**
     * 商家端新增有效期卡
     * @param form
     * @param venueId
     * @return
     */
    @Override
    public ValidCardDto addValidCard(AddValidCardForm form, Integer venueId) {
        boolean exists = new LambdaQueryChainWrapper<ValidCard>(getBaseMapper())
                .eq(ValidCard::getCardName, form.getCardName()).exists();
        if (exists) throw new CardException(80001,"卡名重复");
        ValidCard validCard = BeanUtil.toBean(form, ValidCard.class);
        validCard.setCardState(CardState.IS_ENABLE.getState());
        validCard.setVenueId(venueId);
        save(validCard);
        ValidCardDto dto = BeanUtil.toBean(validCard, ValidCardDto.class);
        return dto;
    }

    /**
     * 商家端禁用有效期会员卡成功
     * @param cardId
     * @param venueId
     */
    @Override
    public void disabledValidCard(Integer cardId, Integer venueId) {
        ValidCard validCard = getById(cardId);
        if (ObjectUtils.isEmpty(validCard)) throw new CardException(80002,"该会员卡不存在");
        if (validCard.getVenueId()!=venueId) throw new CardException(80003,"不是该商家会员卡");
        if (validCard.getCardState()==CardState.IS_DISABLED.getState()) throw new CardException(80004,"该卡已禁用，重复操作");
        validCard.setCardState(CardState.IS_DISABLED.getState());
        updateById(validCard);
    }

    /**
     * 商家端启用有效期会员卡
     * @param cardId
     * @param venueId
     */
    @Override
    public void enableValidCard(Integer cardId, Integer venueId) {
        ValidCard validCard = getById(cardId);
        if (ObjectUtils.isEmpty(validCard)) throw new CardException(80002,"该会员卡不存在");
        if (validCard.getVenueId()!=venueId) throw new CardException(80003,"不是该商家会员卡");
        if (validCard.getCardState()==CardState.IS_ENABLE.getState()) throw new CardException(80004,"该卡已启用，重复操作");
        validCard.setCardState(CardState.IS_ENABLE.getState());
        updateById(validCard);
    }

    /**
     * 商家端修改有效期卡
     * @param form
     * @param venueId
     */
    @Override
    public void updateValidCard(UpdateValidCardForm form, Integer venueId) {
        ValidCard validCard = getById(form.getCardId());
        if (null==validCard) throw new CardException(80002,"该会员卡不存在");
        if (validCard.getVenueId()!=venueId) throw new CardException(80003,"不是该商家会员卡");
        ValidCard one = getOne(new LambdaQueryWrapper<ValidCard>()
                .eq(ValidCard::getCardName, form.getCardName()));
        if (null!=one && one.getCardId()!=form.getCardId()) throw new CardException(80004,"卡名已被占用");
        validCard.setCardName(form.getCardName());
        validCard.setCardPrice(form.getCardPrice());
        validCard.setValidDate(form.getValidDate());
        updateById(validCard);
    }

    /**
     * 根据商家id查找有效期卡信息
     * @param venueId
     * @return
     */
    @Override
    public List<ValidCardDto> findByVenueId(Integer venueId) {
        List<ValidCard> list = list(new LambdaQueryWrapper<ValidCard>()
                .eq(ValidCard::getVenueId, venueId));
        List<ValidCardDto> dtos = BeanUtil.copyToList(list, ValidCardDto.class);
        return dtos;
    }

}
